/**
 * Created by Noir on 1/6/14.
 */

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var chartSchema = new Schema({
    name:{
        type:String,
        required: true
    },
    caption:{
        type:String,
        default: 'updating...'
    },
    description: {
        type:String,
        default: 'updating...'
    },
    createDate: {
        type: Date,
        default: Date.now
    },
    updateDate:{
        type: Date,
        default: Date.now
    },
    viewNumber:{
        type: Number,
        default: 0
    },
    creator:{
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    latestUpdator:{
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    tag:[{
        type: Schema.Types.ObjectId,
        ref: 'Tag'
    }],
    item:[{
        type: Schema.Types.ObjectId,
        ref: 'ChartDetail'
    }],
    followUser:[{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }],
    type:{
        type: String,
        lowercase: true,
        enum: ['open','close','private']
    },
    isPublished:{
        type: Boolean,
        required: true,
        default: false
    }
});

chartSchema.virtual('followNumber').get(function(){
    return this.followUser.length;
});

module.exports = mongoose.model('Chart',chartSchema);
